package com.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.BookService.impl.LendRecordService;
import com.example.demo.commom.Result;
import com.example.demo.entity.LendRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 借阅管理
 */
@RestController
@RequestMapping("/api/LendRecord")
@Slf4j
public class LendRecordController {

    @Resource
    LendRecordService lendRecordService;

    /**
     * 根据图书编号删除
     *
     * @param isbn
     * @return
     */
    @DeleteMapping("/{isbn}")
    public Result<?> delete(@PathVariable String isbn) {
        log.info("根据图书编号删除：{}", isbn);
        lendRecordService.deleteByIsbn(isbn);
        return Result.success();
    }

    /**
     * 删除一条记录
     *
     * @param LendRecord
     * @return
     */
    @PostMapping("/deleteRecord")
    public Result<?> deleteRecord(@RequestBody LendRecord LendRecord) {
        log.info("删除一条借阅记录：{}", LendRecord);
        lendRecordService.deleteRecord(LendRecord);
        return Result.success();
    }

    /**
     * 批量删除记录
     *
     * @param LendRecords
     * @return
     */
    @PostMapping("/deleteRecords")
    public Result<?> deleteRecords(@RequestBody List<LendRecord> LendRecords) {
        log.info("批量删除借阅记录：{}", LendRecords);
        lendRecordService.deleteRecords(LendRecords);
        return Result.success();
    }

    /**
     * 增加一条借阅记录
     *
     * @param LendRecord
     * @return
     */
    @PostMapping
    public Result<?> save(@RequestBody LendRecord LendRecord) {
        log.info("添加一条借阅记录：{}", LendRecord);
        lendRecordService.insert(LendRecord);
        return Result.success();
    }

    /**
     * 分页查询借阅数据
     *
     * @param pageNum  当前页码
     * @param pageSize 每页记录数
     * @param search1  书籍id
     * @param search2  书名
     * @param search3  读者id
     * @return
     */
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search1,
                              @RequestParam(defaultValue = "") String search2,
                              @RequestParam(defaultValue = "") String search3) {
        log.info("分页查询借阅数据，页码：{},总页数：{},图书编号：{},书名：{},读者id：{}", pageNum, pageSize, search1, search2, search3);
        Page<LendRecord> LendRecordPage = lendRecordService.findPage(pageNum, pageSize, search1, search2, search3);
        return Result.success(LendRecordPage);
    }

    /**
     * 根据图书编号更新借阅记录
     *
     * @param isbn
     * @param lendRecord
     * @return
     */
    @PutMapping("/{isbn}")
    public Result<?> update(@PathVariable String isbn, @RequestBody LendRecord lendRecord) {
        log.info("根据图书编号更新借阅记录，图书编号：{},借书记录：{}", isbn, lendRecord);
        lendRecordService.updateByIsbn(isbn, lendRecord);
        return Result.success();
    }

    /**
     * 根据借书时间更新借阅记录
     *
     * @param lendTime
     * @param lendRecord
     * @return
     */
    @PutMapping("/{lendTime}")
    public Result<?> update2(@PathVariable Date lendTime, @RequestBody LendRecord lendRecord) {
        log.info("根据借书时间更新借阅记录，借书时间：{},借阅记录：{}", lendTime, lendRecord);
        lendRecordService.updateByLendtime(lendTime, lendRecord);
        return Result.success();
    }

}
